home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_03 / reader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-08  |  38.3 KB  |  1,711 lines

  1. /*
  2. **    file:        reader.c
  3. **    purpose:    Read the input specification
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9.  
  10. #include "files.h"
  11. #include "new.h"
  12. #include "symtab.h"
  13. #include "lex.h"
  14. #include "output.h"
  15. #include "reader.h"
  16. #include "bison.h"
  17. #include "gram.h"
  18.  
  19. #define LTYPESTR    "\n#ifndef YYLTYPE\ntypedef\n  struct yyltype\n\
  20.     {\n      int timestamp;\n      int first_line;\n      int first_column;\n\
  21.       int last_line;\n      int last_column;\n      char *text;\n   }\n\
  22.   yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
  23.  
  24. /* Number of slots allocated (but not necessarily used yet) in `rline'  */
  25. int rline_allocated = 0;
  26.  
  27. extern int definesflag;
  28. extern int nolinesflag;
  29. extern bucket *symval;
  30. extern int numval;
  31. extern int failure;
  32. extern int expected_conflicts;
  33.  
  34. /*  defined in new.h
  35. extern char *realloc ();
  36. */
  37.  
  38. typedef
  39.   struct symbol_list
  40.     {
  41.       struct symbol_list *next;
  42.       bucket *sym;
  43.       bucket *ruleprec;
  44.     }
  45.   symbol_list;
  46.  
  47.  
  48.  
  49. int lineno;
  50. symbol_list *grammar;
  51. int start_flag;
  52. bucket *startval;
  53. char **tags;
  54.  
  55. static int typed;  /* nonzero if %union has been seen.  */
  56.  
  57. static int lastprec;  /* incremented for each %left, %right or %nonassoc seen */
  58.  
  59. static int gensym_count;  /* incremented for each generated symbol */
  60.  
  61. static bucket *errtoken;
  62.  
  63. /* Nonzero if any action or guard uses the @n construct.  */
  64. static int yylsp_needed;
  65.  
  66. void reader()
  67. {
  68.  
  69.   start_flag = 0;
  70.   startval = NULL;  /* start symbol not specified yet. */
  71.  
  72.   translations = 0;  /* initially assume token number translation not needed.  */
  73.  
  74.   nsyms = 1;
  75.   nvars = 0;
  76.   nrules = 0;
  77.   nitems = 0;
  78.   rline_allocated = 10;
  79.   rline = NEW2(rline_allocated, short);
  80.  
  81.   typed = 0;
  82.   lastprec = 0;
  83.  
  84.   gensym_count = 0;
  85.  
  86.   semantic_parser = 0;
  87.   pure_parser = 0;
  88.   yylsp_needed = 0;
  89.  
  90.   grammar = NULL;
  91.  
  92.   init_lex();
  93.   lineno = 1;
  94.  
  95.   /* initialize the symbol table.  */
  96.   tabinit();
  97.   /* construct the error token */
  98.   errtoken = getsym("error");
  99.   errtoken->class = STOKEN;
  100.   /* construct a token that represents all undefined literal tokens. */
  101.   /* it is always token number 2.  */
  102.   getsym("$illegal.")->class = STOKEN;
  103.   /* Read the declaration section.  Copy %{ ... %} groups to ftable and fdefines file.
  104.      Also notice any %token, %left, etc. found there.  */
  105.   fprintf(ftable, "\n/*  A Bison parser, made from %s  */\n\n", infile);
  106.   read_declarations();
  107.   /* output the definition of YYLTYPE into the fattrs and fdefines files.  */
  108.   output_ltype();
  109.   /* start writing the guard and action files, if they are needed.  */
  110.   output_headers();
  111.   /* read in the grammar, build grammar in list form.  write out guards and actions.  */
  112.   readgram();
  113.   /* write closing delimiters for actions and guards.  */
  114.   output_trailers();
  115.   if (yylsp_needed)
  116.     fprintf(ftable, "#define YYLSP_NEEDED\n\n");
  117.   /* assign the symbols their symbol numbers.
  118.      Write #defines for the token symbols into fdefines if requested.  */
  119.   packsymbols();
  120.   /* convert the grammar into the format described in gram.h.  */
  121.   packgram();
  122.   /* free the symbol table data structure
  123.      since symbols are now all referred to by symbol number.  */
  124.   free_symtab();
  125. }
  126.  
  127.  
  128.  
  129. /* read from finput until %% is seen.  Discard the %%.
  130. Handle any % declarations,
  131. and copy the contents of any %{ ... %} groups to fattrs.  */
  132.  
  133. void read_declarations ()
  134. {
  135.   register int c;
  136.   register int tok;
  137.  
  138.   for (;;)
  139.     {
  140.       c = skip_white_space();
  141.  
  142.       if (c == '%')
  143.     {
  144.       tok = parse_percent_token();
  145.  
  146.       switch (tok)
  147.         {
  148.         case TWO_PERCENTS:
  149.           return;
  150.  
  151.         case PERCENT_LEFT_CURLY:
  152.           copy_definition();
  153.           break;
  154.  
  155.         case TOKEN:
  156.           parse_token_decl (STOKEN, SNTERM);
  157.           break;
  158.     
  159.         case NTERM:
  160.           parse_token_decl (SNTERM, STOKEN);
  161.           break;
  162.     
  163.         case TYPE:
  164.           parse_type_decl();
  165.           break;
  166.     
  167.         case START:
  168.           parse_start_decl();
  169.           break;
  170.     
  171.         case UNION:
  172.           parse_union_decl();
  173.           break;
  174.     
  175.         case EXPECT:
  176.           parse_expect_decl();
  177.           break;
  178.     
  179.         case LEFT:
  180.           parse_assoc_decl(LEFT_ASSOC);
  181.           break;
  182.  
  183.         case RIGHT:
  184.           parse_assoc_decl(RIGHT_ASSOC);
  185.           break;
  186.  
  187.         case NONASSOC:
  188.           parse_assoc_decl(NON_ASSOC);
  189.           break;
  190.  
  191.         case SEMANTIC_PARSER:
  192.           semantic_parser = 1;
  193.           open_extra_files();
  194.           break;
  195.  
  196.         case PURE_PARSER:
  197.           pure_parser = 1;
  198.           break;
  199.  
  200.         default:
  201.           fatal("junk after % in definition section");
  202.         }
  203.     }
  204.       else if (c == EOF)
  205.         fatal("no input grammar");
  206.       else/* JF changed msg */
  207.         fatals("Unrecognized char '%c' in declaration section",c);
  208.  
  209.     }
  210. }
  211.  
  212.  
  213. /* copy the contents of a %{ ... %} into the definitions file.
  214. The %{ has already been read.  Return after reading the %}.  */
  215. void copy_definition ()
  216. {
  217.   register int c;
  218.   register int match;
  219.   register int ended;
  220.   register int after_percent;  /* -1 while reading a character if prev char was % */
  221.  
  222.   if (!nolinesflag)
  223.     fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);
  224.  
  225.   after_percent = 0;
  226.  
  227.   c = getc(finput);
  228.  
  229.   for (;;)
  230.     {
  231.       switch (c)
  232.     {
  233.     case '\n':
  234.       putc(c, fattrs);
  235.       lineno++;
  236.       break;
  237.  
  238.     case '%':
  239.           after_percent = -1;
  240.       break;
  241.           
  242.     case '\'':
  243.     case '"':
  244.       match = c;
  245.       putc(c, fattrs);
  246.       c = getc(finput);
  247.  
  248.       while (c != match)
  249.         {
  250.           if (c == EOF || c == '\n')
  251.         fatal("unterminated string");
  252.  
  253.           putc(c, fattrs);
  254.           
  255.           if (c == '\\')
  256.         {
  257.           c = getc(finput);
  258.           if (c == EOF || c == '\n')
  259.             fatal("unterminated string");
  260.           putc(c, fattrs);
  261.           if (c == '\n')
  262.             lineno++;
  263.         }
  264.  
  265.           c = getc(finput);
  266.         }
  267.  
  268.       putc(c, fattrs);
  269.       break;
  270.  
  271.     case '/':
  272.       putc(c, fattrs);
  273.       c = getc(finput);
  274.       if (c != '*')
  275.         continue;
  276.  
  277.       putc(c, fattrs);
  278.       c = getc(finput);
  279.  
  280.       ended = 0;
  281.       while (!ended)
  282.         {
  283.           if (c == '*')
  284.         {
  285.           while (c == '*')
  286.             {
  287.               putc(c, fattrs);
  288.               c = getc(finput);
  289.             }
  290.  
  291.           if (c == '/')
  292.             {
  293.               putc(c, fattrs);
  294.               ended = 1;
  295.             }
  296.         }
  297.           else if (c == '\n')
  298.         {
  299.           lineno++;
  300.           putc(c, fattrs);
  301.           c = getc(finput);
  302.         }
  303.           else if (c == EOF)
  304.         fatal("unterminated comment in %{ definition");
  305.           else
  306.         {
  307.           putc(c, fattrs);
  308.           c = getc(finput);
  309.         }
  310.         }
  311.  
  312.       break;
  313.  
  314.     case EOF:
  315.       fatal("unterminated %{ definition");
  316.  
  317.     default:
  318.       putc(c, fattrs);
  319.     }
  320.  
  321.       c = getc(finput);
  322.  
  323.       if (after_percent)
  324.     {
  325.       if (c == '}')
  326.         return;
  327.       putc('%', fattrs);
  328.     }
  329.       after_percent = 0;
  330.  
  331.     }
  332.  
  333. }
  334.  
  335.  
  336.  
  337. /* parse what comes after %token or %nterm.
  338. For %token, what_is is STOKEN and what_is_not is SNTERM.
  339. For %nterm, the arguments are reversed.  */
  340.  
  341. void parse_token_decl (what_is, what_is_not)
  342.      int what_is, what_is_not;
  343. {
  344. /*   register int start_lineno; JF */
  345.   register int token = 0;
  346.   register int prev;
  347.   register char *typename = 0;
  348.   int k;
  349.   extern char token_buffer[];
  350.  
  351. /*   start_lineno = lineno; JF */
  352.  
  353.   for (;;)
  354.     {
  355.       if(ungetc(skip_white_space(), finput) == '%')
  356.     return;
  357.  
  358. /*      if (lineno != start_lineno)
  359.     return; JF */
  360.  
  361.       /* we have not passed a newline, so the token now starting is in this declaration */
  362.       prev = token;
  363.  
  364.       token = lex();
  365.       if (token == COMMA)
  366.     continue;
  367.       if (token == TYPENAME)
  368.     {
  369.       k = strlen(token_buffer);
  370.       if (typename) free (typename);
  371.       typename = NEW2(k + 1, char);
  372.       strcpy(typename, token_buffer);
  373.     }
  374.       else if (token == IDENTIFIER)
  375.     {
  376.       if (symval->class == (char) what_is_not)
  377.         fatals("symbol %s redefined", symval->tag);
  378.       symval->class = (char) what_is;
  379.       if (what_is == SNTERM)
  380.         symval->value = nvars++;
  381.  
  382.       if (typename)
  383.         {
  384.           if (symval->type_name == NULL)
  385.         symval->type_name = typename;
  386.           else
  387.         fatals("type redeclaration for %s", symval->tag);
  388.         }
  389.     }
  390.       else if (prev == IDENTIFIER && token == NUMBER)
  391.         {
  392.       symval->user_token_number = numval;
  393.       translations = 1;
  394.         }
  395.       else
  396.     fatal("invalid text in %token or %nterm declaration");
  397.     }
  398.  
  399. }
  400.  
  401.  
  402.  
  403. /* parse what comes after %start */
  404.  
  405. void parse_start_decl ()
  406. {
  407.   if (start_flag)
  408.     fatal("multiple %start declarations");
  409.   start_flag = 1;
  410.   if (lex() != IDENTIFIER)
  411.     fatal("invalid %start declaration");
  412.   startval = symval;
  413. }
  414.  
  415.  
  416.  
  417. /* read in a %type declaration and record its information for get_type_name to access */
  418.  
  419. void parse_type_decl ()
  420. {
  421.   register int k;
  422.   register char *name;
  423. /*   register int start_lineno; JF */
  424.  
  425.   extern char token_buffer[];
  426.  
  427.   if (lex() != TYPENAME)
  428.     fatal("ill-formed %type declaration");
  429.  
  430.   k = strlen(token_buffer);
  431.   name = NEW2(k + 1, char);
  432.   strcpy(name, token_buffer);
  433.  
  434. /*   start_lineno = lineno; */
  435.  
  436.   for (;;)
  437.     {
  438.       register int t;
  439.  
  440.       if(ungetc(skip_white_space(), finput) == '%')
  441.     return;
  442.  
  443. /*       if (lineno != start_lineno)
  444.     return; JF */
  445.  
  446.       /* we have not passed a newline, so the token now starting is in this declaration */
  447.  
  448.       t = lex();
  449.  
  450.       switch (t)
  451.     {
  452.  
  453.     case COMMA:
  454.       break;
  455.  
  456.     case IDENTIFIER:
  457.       if (symval->type_name == NULL)
  458.         symval->type_name = name;
  459.       else
  460.         fatals("type redeclaration for %s", symval->tag);
  461.  
  462.       break;
  463.  
  464.     default:
  465.       fatal("invalid %type declaration");
  466.     }
  467.     }
  468. }
  469.  
  470.  
  471.  
  472. /* read in a %left, %right or %nonassoc declaration and record its information.  */
  473. /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC.  */
  474.  
  475. void parse_assoc_decl (assoc)
  476. int assoc;
  477. {
  478.   register int k;
  479.   register char *name = NULL;
  480. /*  register int start_lineno; JF */
  481.   register int prev = 0;    /* JF added = 0 to keep lint happy */
  482.  
  483.   extern char token_buffer[];
  484.  
  485.   lastprec++;  /* assign a new precedence level.  */
  486.  
  487. /*   start_lineno = lineno; */
  488.  
  489.   for (;;)
  490.     {
  491.       register int t;
  492.  
  493.       if(ungetc(skip_white_space(), finput) == '%')
  494.     return;
  495.  
  496.       /* if (lineno != start_lineno)
  497.     return; JF */
  498.  
  499.       /* we have not passed a newline, so the token now starting is in this declaration */
  500.  
  501.       t = lex();
  502.  
  503.       switch (t)
  504.     {
  505.  
  506.     case TYPENAME:
  507.       k = strlen(token_buffer);
  508.       name = NEW2(k + 1, char);
  509.       strcpy(name, token_buffer);
  510.       break;
  511.  
  512.     case COMMA:
  513.       break;
  514.  
  515.     case IDENTIFIER:
  516.       symval->prec = lastprec;
  517.       symval->assoc = assoc;
  518.       if (symval->class == SNTERM)
  519.         fatals("symbol %s redefined", symval->tag);
  520.       symval->class = STOKEN;
  521.       if (name)
  522.         { /* record the type, if one is specified */
  523.           if (symval->type_name == NULL)
  524.         symval->type_name = name;
  525.           else
  526.         fatals("type redeclaration for %s", symval->tag);
  527.         }
  528.       break;
  529.  
  530.     case NUMBER:
  531.       if (prev == IDENTIFIER)
  532.             {
  533.           symval->user_token_number = numval;
  534.           translations = 1;
  535.             }
  536.           else    
  537.         fatal("invalid text in association declaration");
  538.       break;
  539.  
  540.     case SEMICOLON:
  541.       return;
  542.  
  543.     default:
  544.       fatal("malformatted association declaration");
  545.     }
  546.  
  547.       prev = t;
  548.  
  549.     }
  550. }
  551.  
  552.  
  553.  
  554. /* copy the union declaration into fattrs (and fdefines),
  555.    where it is made into the
  556.    definition of YYSTYPE, the type of elements of the parser value stack.  */
  557.  
  558. void parse_union_decl()
  559. {
  560.   register int c;
  561.   register int count;
  562.   register int in_comment;
  563.  
  564.   if (typed)
  565.     fatal("multiple %union declarations");
  566.  
  567.   typed = 1;
  568.  
  569.   if (!nolinesflag)
  570.     fprintf(fattrs, "\n#line %d \"%s\"\n", lineno, infile);
  571.   else
  572.     fprintf(fattrs, "\n");
  573.  
  574.   fprintf(fattrs, "typedef union");
  575.   if (fdefines)
  576.     fprintf(fdefines, "typedef union");
  577.  
  578.   count = 0;
  579.   in_comment = 0;
  580.  
  581.   c = getc(finput);
  582.  
  583.   while (c != EOF)
  584.     {
  585.       putc(c, fattrs);
  586.       if (fdefines)
  587.     putc(c, fdefines);
  588.  
  589.       switch (c)
  590.     {
  591.     case '\n':
  592.       lineno++;
  593.       break;
  594.  
  595.     case '/':
  596.       c = getc(finput);
  597.       if (c != '*')
  598.         ungetc(c, finput);
  599.       else
  600.         {
  601.           putc('*', fattrs);
  602.           if (fdefines)
  603.         putc('*', fdefines);
  604.           c = getc(finput);
  605.           in_comment = 1;
  606.           while (in_comment)
  607.         {
  608.           if (c == EOF)
  609.             fatal("unterminated comment");
  610.  
  611.           putc(c, fattrs);
  612.           if (fdefines)
  613.             putc(c, fdefines);
  614.           if (c == '*')
  615.             {
  616.               c = getc(finput);
  617.               if (c == '/')
  618.             {
  619.               putc('/', fattrs);
  620.               if (fdefines)
  621.                 putc('/', fdefines);
  622.               in_comment = 0;
  623.             }
  624.             }
  625.           else
  626.             c = getc(finput);
  627.         }
  628.         }
  629.       break;
  630.  
  631.  
  632.     case '{':
  633.       count++;
  634.       break;
  635.  
  636.     case '}':
  637.       count--;
  638.       if (count == 0)
  639.         {
  640.           fprintf(fattrs, " YYSTYPE;\n");
  641.           if (fdefines)
  642.         fprintf(fdefines, " YYSTYPE;\n");
  643.           /* JF don't choke on trailing semi */
  644.           c=skip_white_space();
  645.           if(c!=';') ungetc(c,finput);
  646.           return;
  647.         }
  648.     }
  649.  
  650.       c = getc(finput);
  651.     }
  652. }
  653.  
  654. /* parse the declaration %expect N which says to expect N
  655.    shift-reduce conflicts.  */
  656.  
  657. void parse_expect_decl()
  658. {
  659.   register int c;
  660.   register int count;
  661.   char buffer[20];
  662.  
  663.   c = getc(finput);
  664.   while (c == ' ' || c == '\t')
  665.     c = getc(finput);
  666.  
  667.   count = 0;
  668.   while (c >= '0' && c <= '9')
  669.     {
  670.       if (count < 20)
  671.     buffer[count++] = (char) c;
  672.       c = getc(finput);
  673.     }
  674.  
  675.   ungetc (c, finput);
  676.  
  677.   expected_conflicts = atoi (buffer);
  678. }
  679.  
  680. /* that's all of parsing the declaration section */
  681.  
  682. void output_ltype()
  683. {
  684.   fprintf(fattrs, LTYPESTR);/* JF added YYABORT() */
  685.   if (fdefines)
  686.     fprintf(fdefines, LTYPESTR);/* JF added YYABORT() */
  687.  
  688.   fprintf(fattrs, "#define\tYYACCEPT\treturn(0)\n");
  689.   fprintf(fattrs, "#define\tYYABORT\treturn(1)\n");
  690.   fprintf(fattrs, "#define\tYYERROR\tgoto yyerrlab\n");
  691.  
  692.   if (fdefines)
  693.     {
  694.       fprintf(fdefines, "#define\tYYACCEPT\treturn(0)\n");
  695.       fprintf(fdefines, "#define\tYYABORT\treturn(1)\n");
  696.       fprintf(fdefines, "#define\tYYERROR\tgoto yyerrlab\n");
  697.     }
  698. }
  699.  
  700.  
  701.  
  702. /* Get the data type (alternative in the union) of the value for symbol n in rule rule.  */
  703.  
  704. char *
  705. get_type_name(n, rule)
  706. int n;
  707. symbol_list *rule;
  708. {
  709.   static char *msg = "invalid $ value";
  710.  
  711.   register int i;
  712.   register symbol_list *rp;
  713.  
  714.   if (n < 0)
  715.     fatal(msg);
  716.  
  717.   rp = rule;
  718.   i = 0;
  719.  
  720.   while (i < n)
  721.     {
  722.       rp = rp->next;
  723.       if (rp == NULL || rp->sym == NULL)
  724.     fatal(msg);
  725.       i++;
  726.     }
  727.  
  728.   return (rp->sym->type_name);
  729. }
  730.  
  731.  
  732.  
  733. /* after %guard is seen in the input file,
  734. copy the actual guard into the guards file.
  735. If the guard is followed by an action, copy that into the actions file.
  736. stack_offset is the number of values in the current rule so far,
  737. which says where to find $0 with respect to the top of the stack,
  738. for the simple parser in which the stack is not popped until after the guard is run.  */
  739.  
  740. void copy_guard(rule, stack_offset)
  741. symbol_list *rule;
  742. int stack_offset;
  743. {
  744.   register int c;
  745.   register int n;
  746.   register int count;
  747.   register int match;
  748.   register int ended;
  749.   register char *type_name;
  750.   int brace_flag = 0;
  751.  
  752.   extern char token_buffer[];
  753.  
  754.   /* offset is always 0 if parser has already popped the stack pointer */
  755.   if (semantic_parser) stack_offset = 0;
  756.  
  757.   fprintf(fguard, "\ncase %d:\n", nrules);
  758.   if (!nolinesflag)
  759.     fprintf(fguard, "#line %d \"%s\"\n", lineno, infile);
  760.   putc('{', fguard);
  761.  
  762.   count = 0;
  763.   c = getc(finput);
  764.  
  765.   while (brace_flag ? (count > 0) : (c != ';'))
  766.     {
  767.       switch (c)
  768.     {
  769.     case '\n':
  770.       putc(c, fguard);
  771.       lineno++;
  772.       break;
  773.  
  774.     case '{':
  775.       putc(c, fguard);
  776.       count++;
  777.       break;
  778.  
  779.     case '}':
  780.       putc(c, fguard);
  781.       brace_flag = 1;
  782.       if (count > 0)
  783.         count--;
  784.       else
  785.         fatal("unmatched right brace ('}')");
  786.           break;
  787.  
  788.     case '\'':
  789.     case '"':
  790.       match = c;
  791.       putc(c, fguard);
  792.       c = getc(finput);
  793.  
  794.       while (c != match)
  795.         {
  796.           if (c == EOF || c == '\n')
  797.         fatal("unterminated string");
  798.  
  799.           putc(c, fguard);
  800.           
  801.           if (c == '\\')
  802.         {
  803.           c = getc(finput);
  804.           if (c == EOF || c == '\n')
  805.             fatal("unterminated string");
  806.           putc(c, fguard);
  807.           if (c == '\n')
  808.             lineno++;
  809.         }
  810.  
  811.           c = getc(finput);
  812.         }
  813.  
  814.       putc(c, fguard);
  815.       break;
  816.  
  817.     case '/':
  818.       putc(c, fguard);
  819.       c = getc(finput);
  820.       if (c != '*')
  821.         continue;
  822.  
  823.       putc(c, fguard);
  824.       c = getc(finput);
  825.  
  826.       ended = 0;
  827.       while (!ended)
  828.         {
  829.           if (c == '*')
  830.         {
  831.           while (c == '*')
  832.             {
  833.               putc(c, fguard);
  834.               c = getc(finput);
  835.             }
  836.  
  837.           if (c == '/')
  838.             {
  839.               putc(c, fguard);
  840.               ended = 1;
  841.             }
  842.         }
  843.           else if (c == '\n')
  844.         {
  845.           lineno++;
  846.           putc(c, fguard);
  847.           c = getc(finput);
  848.         }
  849.           else if (c == EOF)
  850.         fatal("unterminated comment");
  851.           else
  852.         {
  853.           putc(c, fguard);
  854.           c = getc(finput);
  855.         }
  856.         }
  857.  
  858.       break;
  859.  
  860.     case '$':
  861.       c = getc(finput);
  862.       type_name = NULL;
  863.  
  864.       if (c == '<')
  865.         {
  866.           register char *cp = token_buffer;
  867.  
  868.           while ((c = getc(finput)) != '>' && c > 0)
  869.         *cp++ = (char) c;
  870.           *cp = 0;
  871.           type_name = token_buffer;
  872.  
  873.           c = getc(finput);
  874.         }
  875.  
  876.       if (c == '$')
  877.         {
  878.           fprintf(fguard, "yyval");
  879.           if (!type_name) type_name = rule->sym->type_name;
  880.           if (type_name)
  881.         fprintf(fguard, ".%s", type_name);
  882.           if(!type_name && typed)   /* JF */
  883.         fprintf(stderr,"%s:%d:  warning:  $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  884.         }
  885.  
  886.       else if (isdigit(c) || c == '-')
  887.         {
  888.           ungetc (c, finput);
  889.           n = read_signed_integer(finput);
  890.           c = getc(finput);
  891.  
  892.           if (!type_name && n > 0)
  893.         type_name = get_type_name(n, rule);
  894.  
  895.           fprintf(fguard, "yyvsp[%d]", n - stack_offset);
  896.           if (type_name)
  897.         fprintf(fguard, ".%s", type_name);
  898.           if(!type_name && typed)   /* JF */
  899.         fprintf(stderr,"%s:%d:  warning:  $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  900.           continue;
  901.         }
  902.       else
  903.         fatals("$%c is invalid",c); /* JF changed style */
  904.  
  905.       break;
  906.  
  907.     case '@':
  908.       c = getc(finput);
  909.       if (isdigit(c) || c == '-')
  910.         {
  911.           ungetc (c, finput);
  912.           n = read_signed_integer(finput);
  913.           c = getc(finput);
  914.         }
  915.       else
  916.         fatals("@%c is invalid",c); /* JF changed style */
  917.  
  918.       fprintf(fguard, "yylsp[%d]", n - stack_offset);
  919.       yylsp_needed = 1;
  920.  
  921.       continue;
  922.  
  923.     case EOF:
  924.       fatal("unterminated %guard clause");
  925.  
  926.     default:
  927.       putc(c, fguard);
  928.     }
  929.  
  930.       if (c != '}' || count != 0)
  931.     c = getc(finput);
  932.     }
  933.  
  934.   c = skip_white_space();
  935.  
  936.   fprintf(fguard, ";\n    break;}");
  937.   if (c == '{')
  938.     copy_action(rule, stack_offset);
  939.   else if (c == '=')
  940.     {
  941.       c = getc(finput);
  942.       if (c == '{')
  943.     copy_action(rule, stack_offset);
  944.     }
  945.   else
  946.     ungetc(c, finput);
  947. }
  948.  
  949.  
  950.  
  951. /* Assuming that a { has just been seen, copy everything up to the matching }
  952. into the actions file.
  953. stack_offset is the number of values in the current rule so far,
  954. which says where to find $0 with respect to the top of the stack.  */
  955.  
  956. void copy_action(rule, stack_offset)
  957. symbol_list *rule;
  958. int stack_offset;
  959. {
  960.   register int c;
  961.   register int n;
  962.   register int count;
  963.   register int match;
  964.   register int ended;
  965.   register char *type_name;
  966.   extern char token_buffer[];
  967.  
  968.   /* offset is always 0 if parser has already popped the stack pointer */
  969.   if (semantic_parser) stack_offset = 0;
  970.  
  971.   fprintf(faction, "\ncase %d:\n", nrules);
  972.   if (!nolinesflag)
  973.     fprintf(faction, "#line %d \"%s\"\n", lineno, infile);
  974.   putc('{', faction);
  975.  
  976.   count = 1;
  977.   c = getc(finput);
  978.  
  979.   while (count > 0)
  980.     {
  981.       while (c != '}')
  982.         {
  983.           switch (c)
  984.         {
  985.         case '\n':
  986.           putc(c, faction);
  987.           lineno++;
  988.           break;
  989.  
  990.         case '{':
  991.           putc(c, faction);
  992.           count++;
  993.           break;
  994.  
  995.         case '\'':
  996.         case '"':
  997.           match = c;
  998.           putc(c, faction);
  999.           c = getc(finput);
  1000.  
  1001.           while (c != match)
  1002.         {
  1003.           if (c == EOF || c == '\n')
  1004.             fatal("unterminated string");
  1005.  
  1006.           putc(c, faction);
  1007.  
  1008.           if (c == '\\')
  1009.             {
  1010.               c = getc(finput);
  1011.               if (c == EOF)
  1012.             fatal("unterminated string");
  1013.               putc(c, faction);
  1014.               if (c == '\n')
  1015.             lineno++;
  1016.             }
  1017.  
  1018.           c = getc(finput);
  1019.         }
  1020.  
  1021.           putc(c, faction);
  1022.           break;
  1023.  
  1024.         case '/':
  1025.           putc(c, faction);
  1026.           c = getc(finput);
  1027.           if (c != '*')
  1028.         continue;
  1029.  
  1030.           putc(c, faction);
  1031.           c = getc(finput);
  1032.  
  1033.           ended = 0;
  1034.           while (!ended)
  1035.         {
  1036.           if (c == '*')
  1037.             {
  1038.               while (c == '*')
  1039.                 {
  1040.               putc(c, faction);
  1041.               c = getc(finput);
  1042.             }
  1043.  
  1044.               if (c == '/')
  1045.             {
  1046.               putc(c, faction);
  1047.               ended = 1;
  1048.             }
  1049.             }
  1050.           else if (c == '\n')
  1051.             {
  1052.               lineno++;
  1053.               putc(c, faction);
  1054.               c = getc(finput);
  1055.             }
  1056.           else if (c == EOF)
  1057.             fatal("unterminated comment");
  1058.           else
  1059.             {
  1060.               putc(c, faction);
  1061.               c = getc(finput);
  1062.             }
  1063.         }
  1064.  
  1065.           break;
  1066.  
  1067.         case '$':
  1068.           c = getc(finput);
  1069.           type_name = NULL;
  1070.  
  1071.           if (c == '<')
  1072.         {
  1073.           register char *cp = token_buffer;
  1074.  
  1075.           while ((c = getc(finput)) != '>' && c > 0)
  1076.             *cp++ = (char) c;
  1077.           *cp = (char) 0;
  1078.           type_name = token_buffer;
  1079.  
  1080.           c = getc(finput);
  1081.         }
  1082.           if (c == '$')
  1083.         {
  1084.           fprintf(faction, "yyval");
  1085.           if (!type_name) type_name = get_type_name(0, rule);
  1086.           if (type_name)
  1087.             fprintf(faction, ".%s", type_name);
  1088.           if(!type_name && typed)   /* JF */
  1089.             fprintf(stderr,"%s:%d:  warning:  $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  1090.         }
  1091.           else if (isdigit(c) || c == '-')
  1092.         {
  1093.           ungetc (c, finput);
  1094.           n = read_signed_integer(finput);
  1095.           c = getc(finput);
  1096.  
  1097.           if (!type_name && n > 0)
  1098.             type_name = get_type_name(n, rule);
  1099.  
  1100.           fprintf(faction, "yyvsp[%d]", n - stack_offset);
  1101.           if (type_name)
  1102.             fprintf(faction, ".%s", type_name);
  1103.           if(!type_name && typed)   /* JF */
  1104.             fprintf(stderr,"%s:%d:  warning:  $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  1105.           continue;
  1106.         }
  1107.           else
  1108.         fatals("$%c is invalid",c); /* JF changed format */
  1109.  
  1110.           break;
  1111.  
  1112.         case '@':
  1113.           c = getc(finput);
  1114.           if (isdigit(c) || c == '-')
  1115.         {
  1116.           ungetc (c, finput);
  1117.           n = read_signed_integer(finput);
  1118.           c = getc(finput);
  1119.         }
  1120.           else
  1121.         fatal("invalid @-construct");
  1122.  
  1123.           fprintf(faction, "yylsp[%d]", n - stack_offset);
  1124.           yylsp_needed = 1;
  1125.  
  1126.           continue;
  1127.  
  1128.         case EOF:
  1129.           fatal("unmatched '{'");
  1130.  
  1131.         default:
  1132.           putc(c, faction);
  1133.         }
  1134.  
  1135.           c = getc(finput);
  1136.         }
  1137.  
  1138.       /* above loop exits when c is '}' */
  1139.  
  1140.       if (--count)
  1141.         {
  1142.       putc(c, faction);
  1143.       c = getc(finput);
  1144.     }
  1145.     }
  1146.  
  1147.   fprintf(faction, ";\n    break;}");
  1148. }
  1149.  
  1150.  
  1151.  
  1152. /* generate a dummy symbol, a nonterminal,
  1153. whose name cannot conflict with the user's names. */
  1154.  
  1155. bucket *
  1156. gensym()
  1157. {
  1158.   register bucket *sym;
  1159.  
  1160.   extern char token_buffer[];
  1161.   sprintf (token_buffer, "@%d", ++gensym_count);
  1162.   sym = getsym(token_buffer);
  1163.   sym->class = SNTERM;
  1164.   sym->value = nvars++;
  1165.   return (sym);
  1166. }
  1167.  
  1168.  
  1169.  
  1170. /* Parse the input grammar into a one symbol_list structure.
  1171. Each rule is represented by a sequence of symbols: the left hand side
  1172. followed by the contents of the right hand side, followed by a null pointer
  1173. instead of a symbol to terminate the rule.
  1174. The next symbol is the lhs of the following rule.
  1175.  
  1176. All guards and actions are copied out to the appropriate files,
  1177. labelled by the rule number they apply to.  */
  1178.  
  1179. void readgram()
  1180. {
  1181.   register int t;
  1182.   register bucket *lhs;
  1183.   register symbol_list *p;
  1184.   register symbol_list *p1;
  1185.   register bucket *bp;
  1186.  
  1187.   symbol_list *crule;   /* points to first symbol_list of current rule.  */
  1188.             /* its symbol is the lhs of the rule.   */
  1189.   symbol_list *crule1;  /* points to the symbol_list preceding crule.  */
  1190.  
  1191.   p1 = NULL;
  1192.  
  1193.   t = lex();
  1194.  
  1195.   while (t != TWO_PERCENTS && t != ENDFILE)
  1196.     {
  1197.       if (t == IDENTIFIER || t == BAR)
  1198.     {
  1199.       register int actionflag = 0;
  1200.       int rulelength = 0;  /* number of symbols in rhs of this rule so far  */
  1201.       int xactions = 0; /* JF for error checking */
  1202.       bucket *first_rhs = 0;
  1203.  
  1204.       if (t == IDENTIFIER)
  1205.         {
  1206.           lhs = symval;
  1207.     
  1208.           t = lex();
  1209.           if (t != COLON)
  1210.         fatal("ill-formed rule");
  1211.         }
  1212.  
  1213.       if (nrules == 0)
  1214.         {
  1215.           if (t == BAR)
  1216.         fatal("grammar starts with vertical bar");
  1217.  
  1218.           if (!start_flag)
  1219.         startval = lhs;
  1220.         }
  1221.  
  1222.       /* start a new rule and record its lhs.  */
  1223.  
  1224.       nrules++;
  1225.       nitems++;
  1226.  
  1227.       record_rule_line ();
  1228.  
  1229.       p = NEW(symbol_list);
  1230.       p->sym = lhs;
  1231.  
  1232.       crule1 = p1;
  1233.       if (p1)
  1234.         p1->next = p;
  1235.       else
  1236.         grammar = p;
  1237.  
  1238.       p1 = p;
  1239.       crule = p;
  1240.  
  1241.       /* mark the rule's lhs as a nonterminal if not already so.  */
  1242.  
  1243.       if (lhs->class == SUNKNOWN)
  1244.         {
  1245.           lhs->class = SNTERM;
  1246.           lhs->value = nvars;
  1247.           nvars++;
  1248.         }
  1249.       else if (lhs->class == STOKEN)
  1250.         fatals("rule given for %s, which is a token", lhs->tag);
  1251.  
  1252.       /* read the rhs of the rule.  */
  1253.  
  1254.       for (;;)
  1255.         {
  1256.           t = lex();
  1257.  
  1258.           if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;
  1259.  
  1260.           /* if next token is an identifier, see if a colon follows it.
  1261.          If one does, exit this rule now.  */
  1262.           if (t == IDENTIFIER)
  1263.         {
  1264.           register bucket *ssave;
  1265.           register int t1;
  1266.  
  1267.           ssave = symval;
  1268.           t1 = lex();
  1269.           unlex(t1);
  1270.           symval = ssave;
  1271.           if (t1 == COLON) break;
  1272.  
  1273.           if(!first_rhs)    /* JF */
  1274.             first_rhs = symval;
  1275.           /* not followed by colon => process as part of this rule's rhs.  */
  1276.           if (actionflag)
  1277.             {
  1278.               register bucket *sdummy;
  1279.  
  1280.               /* if this symbol was preceded by an action, */
  1281.               /* make a dummy nonterminal to replace that action in this rule */
  1282.               /* and make another rule to associate the action to the dummy.  */
  1283.               /* Since the action was written out with this rule's number, */
  1284.               /* we must write give the new rule this number */
  1285.               /* by inserting the new rule before it.  */
  1286.  
  1287.               /* make a dummy nonterminal, a gensym.  */
  1288.               sdummy = gensym();
  1289.  
  1290.               /* make a new rule, whose body is empty, before the current one.  */
  1291.               /* so that the action just read can belong to it.  */
  1292.               nrules++;
  1293.               nitems++;
  1294.               record_rule_line ();
  1295.               p = NEW(symbol_list);
  1296.               if (crule1)
  1297.             crule1->next = p;
  1298.               else grammar = p;
  1299.               p->sym = sdummy;
  1300.               crule1 = NEW(symbol_list);
  1301.               p->next = crule1;
  1302.               crule1->next = crule;
  1303.               
  1304.               /* insert the dummy generated by that rule into this rule.  */
  1305.               nitems++;
  1306.               p = NEW(symbol_list);
  1307.               p->sym = sdummy;
  1308.               p1->next = p;
  1309.               p1 = p;
  1310.  
  1311.               actionflag = 0;
  1312.             }
  1313.           nitems++;
  1314.           p = NEW(symbol_list);
  1315.           p->sym = symval;
  1316.           p1->next = p;
  1317.           p1 = p;
  1318.         }
  1319.           else /* handle an action.  */
  1320.         {
  1321.           copy_action(crule, rulelength);
  1322.           actionflag = 1;
  1323.           xactions++;   /* JF */
  1324.         }
  1325.           rulelength++;
  1326.         }
  1327.  
  1328.       /* Put an empty link in the list to mark the end of this rule  */
  1329.       p = NEW(symbol_list);
  1330.       p1->next = p;
  1331.       p1 = p;
  1332.  
  1333.       if (t == PREC)
  1334.         {
  1335.           t = lex();
  1336.           crule->ruleprec = symval;
  1337.           t = lex();
  1338.         }
  1339.       if (t == GUARD)
  1340.         {
  1341.           if (! semantic_parser)
  1342.         fatal("%guard present but %semantic_parser not specified");
  1343.  
  1344.           copy_guard(crule, rulelength);
  1345.           t = lex();
  1346.         }
  1347.       else if (t == LEFT_CURLY)
  1348.         {
  1349.           if (actionflag) fatal("two actions at end of one rule");
  1350.           copy_action(crule, rulelength);
  1351.           t = lex();
  1352.         }
  1353.       /* JF if we'd end up using default, get a warning */
  1354.       else if(!xactions && first_rhs && lhs->type_name!=first_rhs->type_name) {
  1355.         if(lhs->type_name == 0 || first_rhs->type_name == 0 ||
  1356.                       strcmp(lhs->type_name,first_rhs->type_name))
  1357.           fprintf(stderr,"%s:%d:  warning:  type clash ('%s' '%s') on default action\n",
  1358.               infile,
  1359.               lineno,
  1360.               lhs->type_name ? lhs->type_name : "",
  1361.               first_rhs->type_name ? first_rhs->type_name : "");
  1362.       }
  1363.       if (t == SEMICOLON)
  1364.         t = lex();
  1365.     }
  1366.       /* these things can appear as alternatives to rules.  */
  1367.       else if (t == TOKEN)
  1368.     {
  1369.       parse_token_decl(STOKEN, SNTERM);
  1370.       t = lex();
  1371.     }
  1372.       else if (t == NTERM)
  1373.     {
  1374.       parse_token_decl(SNTERM, STOKEN);
  1375.       t = lex();
  1376.     }
  1377.       else if (t == TYPE)
  1378.     {
  1379.       t = get_type();
  1380.     }
  1381.       else if (t == UNION)
  1382.     {
  1383.       parse_union_decl();
  1384.       t = lex();
  1385.     }
  1386.       else if (t == EXPECT)
  1387.     {
  1388.       parse_expect_decl();
  1389.       t = lex();
  1390.     }
  1391.       else if (t == START)
  1392.     {
  1393.       parse_start_decl();
  1394.       t = lex();
  1395.     }
  1396.       else
  1397.     fatal("invalid input");
  1398.     }
  1399.  
  1400.   if (nrules == 0)
  1401.     fatal("no input grammar");
  1402.  
  1403.   if (typed == 0)/* JF put out same default YYSTYPE as YACC does */
  1404.     {
  1405.       fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1406.       if (fdefines)
  1407.     fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1408.     }
  1409.  
  1410.   /* Report any undefined symbols and consider them nonterminals.  */
  1411.  
  1412.   for (bp = firstsymbol; bp; bp = bp->next)
  1413.     if (bp->class == SUNKNOWN)
  1414.       {
  1415.     fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
  1416.             bp->tag);
  1417.     failure = 1;
  1418.     bp->class = SNTERM;
  1419.     bp->value = nvars++;
  1420.       }
  1421.  
  1422.   ntokens = nsyms - nvars;
  1423. }
  1424.  
  1425.  
  1426. void record_rule_line ()
  1427. {
  1428.   /* Record each rule's source line number in rline table.  */
  1429.  
  1430.   if (nrules >= rline_allocated)
  1431.     {
  1432.       rline_allocated = nrules * 2;
  1433.       rline = (short *) realloc (rline,
  1434.                  rline_allocated * sizeof (short));
  1435.       if (rline == 0)
  1436.     {
  1437.       fprintf (stderr, "bison: memory exhausted\n");
  1438.       done (1);
  1439.     }
  1440.     }
  1441.   rline[nrules] = lineno;
  1442. }
  1443.  
  1444.  
  1445. /* read in a %type declaration and record its information for get_type_name to access */
  1446.  
  1447. int
  1448. get_type()
  1449. {
  1450.   register int k;
  1451.   register int t;
  1452.   register char *name;
  1453.  
  1454.   extern char token_buffer[];
  1455.  
  1456.   t = lex();
  1457.  
  1458.   if (t != TYPENAME)
  1459.     fatal("ill-formed %type declaration");
  1460.  
  1461.   k = strlen(token_buffer);
  1462.   name = NEW2(k + 1, char);
  1463.   strcpy(name, token_buffer);
  1464.  
  1465.   for (;;)
  1466.     {
  1467.       t = lex();
  1468.  
  1469.       switch (t)
  1470.     {
  1471.     case SEMICOLON:
  1472.       return (lex());
  1473.  
  1474.     case COMMA:
  1475.       break;
  1476.  
  1477.     case IDENTIFIER:
  1478.       if (symval->type_name == NULL)
  1479.         symval->type_name = name;
  1480.       else
  1481.         fatals("type redeclaration for %s", symval->tag);
  1482.  
  1483.       break;
  1484.  
  1485.     default:
  1486.       return (t);
  1487.     }
  1488.     }
  1489. }
  1490.  
  1491.  
  1492.  
  1493. /* assign symbol numbers, and write definition of token names into fdefines.
  1494. Set up vectors tags and sprec of names and precedences of symbols.  */
  1495.  
  1496. void packsymbols()
  1497. {
  1498.   register bucket *bp;
  1499.   register int tokno = 1;
  1500.   register int i;
  1501.   register int last_user_token_number;
  1502.  
  1503.   /* int lossage = 0; JF set but not used */
  1504.  
  1505.   tags = NEW2(nsyms + 1, char *);
  1506.   tags[0] = "$";
  1507.  
  1508.   sprec = NEW2(nsyms, short);
  1509.   sassoc = NEW2(nsyms, short);
  1510.  
  1511.   max_user_token_number = 255;
  1512.   last_user_token_number = 255;
  1513.  
  1514.   for (bp = firstsymbol; bp; bp = bp->next)
  1515.     {
  1516.       if (bp->class == SNTERM)
  1517.     {
  1518.       bp->value += ntokens;
  1519.     }
  1520.       else
  1521.     {
  1522.       if (translations && !(bp->user_token_number))
  1523.         bp->user_token_number = ++last_user_token_number;
  1524.       if (bp->user_token_number > max_user_token_number)
  1525.         max_user_token_number = bp->user_token_number;
  1526.       bp->value = tokno++;
  1527.     }
  1528.  
  1529.       tags[bp->value] = bp->tag;
  1530.       sprec[bp->value] = bp->prec;
  1531.       sassoc[bp->value] = bp->assoc;
  1532.  
  1533.     }
  1534.  
  1535.   if (translations)
  1536.     {
  1537.       register int i;
  1538.  
  1539.       token_translations = NEW2(max_user_token_number+1, short);
  1540.  
  1541.       /* initialize all entries for literal tokens to 2,
  1542.      the internal token number for $illegal., which represents all invalid inputs.  */
  1543.       for (i = 0; i <= max_user_token_number; i++)
  1544.         token_translations[i] = 2;      
  1545.     }
  1546.  
  1547.   for (bp = firstsymbol; bp; bp = bp->next)
  1548.     {
  1549.       if (bp->value >= ntokens) continue;
  1550.       if (translations)
  1551.     {
  1552.       if (token_translations[bp->user_token_number] != 2)
  1553.         {
  1554.             /* JF made this a call to fatals() */
  1555.           fatals( "tokens %s and %s both assigned number %d",
  1556.                   tags[token_translations[bp->user_token_number]],
  1557.                   bp->tag,
  1558.                   bp->user_token_number);
  1559.         }
  1560.       token_translations[bp->user_token_number] = bp->value;
  1561.     }
  1562.     }
  1563.  
  1564.   error_token_number = errtoken->value;
  1565.  
  1566.   output_token_defines(ftable);
  1567.  
  1568.   if (startval->class == SUNKNOWN)
  1569.     fatals("the start symbol %s is undefined", startval->tag);
  1570.   else if (startval->class == STOKEN)
  1571.     fatals("the start symbol %s is a token", startval->tag);
  1572.  
  1573.   start_symbol = startval->value;
  1574.  
  1575.   if (definesflag)
  1576.     {
  1577.       output_token_defines(fdefines);
  1578.  
  1579.       if (semantic_parser)
  1580.     for (i = ntokens; i < nsyms; i++)
  1581.       {
  1582.         /* don't make these for dummy nonterminals made by gensym.  */
  1583.         if (*tags[i] != '@')
  1584.           fprintf(fdefines, "#define\tNT%s\t%d\n", tags[i], i);
  1585.       }
  1586.  
  1587.       fclose(fdefines);
  1588.       fdefines = NULL;
  1589.     }
  1590. }
  1591.       
  1592.  
  1593. void output_token_defines(file)
  1594. FILE *file;
  1595. {
  1596.   bucket *bp;
  1597.  
  1598.   for (bp = firstsymbol; bp; bp = bp->next)
  1599.     {
  1600.       if (bp->value >= ntokens) continue;
  1601.  
  1602.       /* For named tokens, but not literal ones, define the name.  */
  1603.       /* The value is the user token number.  */
  1604.  
  1605.       if ('\'' != *tags[bp->value] && bp != errtoken)
  1606.     {
  1607.       register char *cp = tags[bp->value];
  1608.       register char c;
  1609.  
  1610.       /* Don't #define nonliteral tokens whose names contain periods.  */
  1611.  
  1612.       while ((c = *cp++) && c != '.');
  1613.       if (!c)
  1614.         {
  1615.               fprintf(file, "#define\t%s\t%d\n", tags[bp->value],
  1616.                 (translations ? bp->user_token_number : bp->value));
  1617.           if (semantic_parser)
  1618.                 fprintf(file, "#define\tT%s\t%d\n", tags[bp->value],
  1619.                   bp->value);
  1620.         }
  1621.     }
  1622.     }
  1623.  
  1624.   putc('\n', file);
  1625. }
  1626.  
  1627.  
  1628.  
  1629. /* convert the rules into the representation using rrhs, rlhs and ritems.  */
  1630.  
  1631. void packgram()
  1632. {
  1633.   register int itemno;
  1634.   register int ruleno;
  1635.   register symbol_list *p;
  1636. /*  register bucket *bp; JF unused */
  1637.  
  1638.   bucket *ruleprec;
  1639.  
  1640.   ritem = NEW2(nitems + 1, short);
  1641.   rlhs = NEW2(nrules, short) - 1;
  1642.   rrhs = NEW2(nrules, short) - 1;
  1643.   rprec = NEW2(nrules, short) - 1;
  1644.   rassoc = NEW2(nrules, short) - 1;
  1645.  
  1646.   itemno = 0;
  1647.   ruleno = 1;
  1648.  
  1649.   p = grammar;
  1650.   while (p)
  1651.     {
  1652.       rlhs[ruleno] = p->sym->value;
  1653.       rrhs[ruleno] = itemno;
  1654.       ruleprec = p->ruleprec;
  1655.  
  1656.       p = p->next;
  1657.       while (p && p->sym)
  1658.     {
  1659.       ritem[itemno++] = p->sym->value;
  1660.       /* a rule gets the precedence and associativity of the last token in it.  */
  1661.           if (p->sym->class == STOKEN)
  1662.         {
  1663.           rprec[ruleno] = p->sym->prec;
  1664.           rassoc[ruleno] = p->sym->assoc;
  1665.         }
  1666.       if (p) p = p->next;
  1667.     }
  1668.  
  1669.       /* if this rule has a %prec, specified symbol's precedence replaces the default */
  1670.       if (ruleprec)
  1671.     {
  1672.           rprec[ruleno] = ruleprec->prec;
  1673.           rassoc[ruleno] = ruleprec->assoc;
  1674.     }
  1675.  
  1676.       ritem[itemno++] = -ruleno;
  1677.       ruleno++;
  1678.  
  1679.       if (p) p = p->next;
  1680.     }
  1681.  
  1682.   ritem[itemno] = 0;
  1683. }
  1684.  
  1685. /* Read a signed integer from STREAM and return its value.  */
  1686.  
  1687. int read_signed_integer (stream)
  1688.      FILE *stream;
  1689. {
  1690.   register int c = getc(stream);
  1691.   register int sign = 1;
  1692.   register int n;
  1693.  
  1694.   if (c == '-')
  1695.     {
  1696.       c = getc(stream);
  1697.       sign = -1;
  1698.     }
  1699.   n = 0;
  1700.   while (isdigit(c))
  1701.     {
  1702.       n = 10*n + (c - '0');
  1703.       c = getc(stream);
  1704.     }
  1705.  
  1706.   ungetc(c, stream);
  1707.  
  1708.   return n * sign;
  1709. }
  1710.  
  1711.